<?php
declare (strict_types=1);

namespace app\platform\model;

use think\Collection;
use think\Model;

class Role extends Model
{
    public function getAllRole(array $where = []): Collection
    {
        $where['is_delete'] = config('status.mysql.table_normal');
        return $this->where($where)->select();
    }

    public function getRoleList(int $page = 1, int $limit = 10, array $where = []): Collection
    {
        $offset             = ($page - 1) * $limit;
        $where['is_delete'] = config('status.mysql.table_normal');
        return $this->where($where)->limit($offset, $limit)->select();
    }

    public function getRoleCount(array $where = []): int
    {
        $where['is_delete'] = config('status.mysql.table_normal');
        return $this->where($where)->count();
    }

    public function findRoleById(int $id)
    {
        return $this->where(["id" => $id, 'is_delete' => config('status.mysql.table_normal')])->find();
    }

    public function addRole(array $data): bool
    {
        $data['is_delete'] = config('status.mysql.table_normal');
        return (boolean)$this->save($data);
    }

    public function updateRole(int $id, array $data): bool
    {
        $data['updated_at'] = date('Y-m-d H:i:s');
        return (boolean)$this->where(['id' => $id])->save($data);
    }

    public function deleteRole(int $id): bool
    {
        return (boolean)$this->where(['id' => $id])->save(['is_delete' => config('status.mysql.table_delete')]);
    }
}
